﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DevExpress.MailClient.Win.Forms
{
    public partial class frmChooseEmailUsers : PRO_DOCS.BLL.WIN.Templates.DevxPermissionTemplate
    {
        public TextSelections CURRENT_TEXT_SELECTION
        {
            get;
            set;
        }

        public enum TextSelections
        {
            To = 0,
            Cc,
            Bcc
        }


        public Dictionary<long, string> SELECTED_EMAIL_TO_LIST
        { get; set; }

        public frmChooseEmailUsers(Dictionary<long, string> sELECTED_EMAIL_TO_LIST)
        {
            InitializeComponent();

            SELECTED_EMAIL_TO_LIST = sELECTED_EMAIL_TO_LIST;

            List<PRO_DOCS.DAL.Database.LOGIN_USER> availableLoginUsers = (from users in PRO_DOCSDatabaseDataContext.LOGIN_USERs
                                                                          where users.IS_ACTIVE_USER.Value.Equals(true)
                                                                          select users).ToList();

            List<Classes.UsersSelections> aVAILABLE_USERS = new List<Classes.UsersSelections>();

            for (int i = 0; i < availableLoginUsers.Count; i++)
            {
                Classes.UsersSelections singleUser = new Classes.UsersSelections();

                singleUser.USER_IS_SELECTED = SELECTED_EMAIL_TO_LIST.ContainsKey(availableLoginUsers[i].LOGIN_USER_ID);

                singleUser.LOGIN_USER = availableLoginUsers[i];

                aVAILABLE_USERS.Add(singleUser);
            }

            gridControl1.DataSource = aVAILABLE_USERS;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            gridView1.CloseEditor();

            SELECTED_EMAIL_TO_LIST = new Dictionary<long, string>();

            for (int i = 0; i < gridView1.DataRowCount; i++)
            {
                Classes.UsersSelections singleUser = gridView1.GetRow(i) as Classes.UsersSelections;

                if (singleUser.USER_IS_SELECTED)
                {
                    SELECTED_EMAIL_TO_LIST.Add(singleUser.LOGIN_USER.LOGIN_USER_ID, singleUser.LOGIN_USER.EMPLOYEE_FULL_NAME);
                }
            } 
            
            this.DialogResult = DialogResult.OK;
        }
    }
}